#ifndef __DPAD_CONTROLS_H__
#define __DPAD_CONTROLS_H__

// Includes
#include <PA9.h>       // Include for PA_Lib


#include "Avatar.h"
#include "Init.h"
#include "SeamlessMap.h"
#include "Spawn.h"


//=====PAB=====//

u8 CheckCol(u8 Cx, u8 Cy) //check x, check y
{
	u16 i = PA_EasyBgGetPixelCol(0,2,PA_GetSpriteX(0,avSprNo)+Cx,PA_GetSpriteY(0,avSprNo)+Cy);

	if (i==black)
		return 1;
	else
		return 0;
}

//chest finding function
void checkChest(int d) //chest x, chest y, direction
{
	u16 k;
	switch(d)
	{
	case 1:	  //right
		for (u8 i=0;i<191;i++)
		{
			k = PA_EasyBgGetPixelCol(0,2,255,i);
			if (k==yellow) 
			{
				
				chest1 = true;
			}
		}
		break;
		
	case 2: // left
		for (u8 i=0;i<191;i++)
		{
			k = PA_EasyBgGetPixelCol(0,2,1,i);
			if (k==yellow) 
			{
			
				chest1 = true;
			}
		}
	
		break;
		
	case 3:   //up
		for (u8 i=0;i<255;i++)
		{
			k = PA_EasyBgGetPixelCol(0,2,0,i);
			if (k==yellow) 
			{
				chest1 = true;
			}
		}
		break;
		
	case 4:  //down
		for (u8 i=0;i<255;i++)
		{
			k = PA_EasyBgGetPixelCol(0,2,191,i);
			if (k==yellow) 
			{
				chest1 = true;
			}
		}
		break;
		
	default: break;
	
	}
}//=====PAB=====//





void DPadCtrl()
{
	
/************ D-PAD ACTIONS **************/
//------------------------------------------------------------
	if ((Pad.Newpress.Right) || (Pad.Newpress.A))
	{
	//pause stamina clock while moving
		PauseTimer(staminaClock); 
		
		PA_StartSpriteAnim(0, //Screen
							avSprNo, //sprite number
							1, //first frame
							6, //last frame
							7); //speed 5fps
		
		direction = 1; //set direction
		moving = true; //turn on flag
	}	
	
	if ((Pad.Newpress.Left)  || (Pad.Newpress.Y))
	{
		PauseTimer(staminaClock);
		PA_StartSpriteAnim(0, avSprNo, 8, 13, 7);
		direction = 2;
		moving = true;
	}	
	
	if ((Pad.Newpress.Up) || (Pad.Newpress.X))
	{
		PauseTimer(staminaClock);
		PA_StartSpriteAnim(0, avSprNo, 56, 61, 7);
		direction = 3;
		moving = true;
	}
	
	if ((Pad.Newpress.Down) || (Pad.Newpress.B))
	{
		PauseTimer(staminaClock);
		PA_StartSpriteAnim(0, avSprNo, 15, 20, 7);
		direction = 4;
		moving = true;
	}
	
//IF RELEASED	
	if ((Pad.Released.Right) || (Pad.Released.A) || (Pad.Released.Left) || (Pad.Released.Y) ||
	(Pad.Released.Up) || (Pad.Released.X) || (Pad.Released.Down) || (Pad.Released.B))
	{
	//unpause stamina clock when player stops/rests
		UnpauseTimer(staminaClock);  
	}
	
	
//IF NO LONGER HELD, STOP ANIMATION
	if(!((Pad.Held.Right)||(Pad.Held.Left)||(Pad.Held.Up)||(Pad.Held.Down)||
			(Pad.Held.A)||(Pad.Held.Y)||(Pad.Held.X)||(Pad.Held.B)) && (moving == true)) 
	{
		
		switch(direction)
		{
		case 1:
		//facing right
			PA_SetSpriteAnim(0, avSprNo, 0); //set animation frame
			break;
		case 2:
		//facing left
			PA_SetSpriteAnim(0, avSprNo, 7);
			break;
		case 3:
		//facing back
			PA_SetSpriteAnim(0, avSprNo, 55);
			break;
		case 4:
		//facing forward
			PA_SetSpriteAnim(0, avSprNo, 14);
			break;
		default:
			break;
		}
		
	}
	
	
	
	//=====PAB=====//
	//coll detection debug
	u16 i = PA_EasyBgGetPixelCol(1,2,PA_GetSpriteX(0,avSprNo),PA_GetSpriteY(0,avSprNo));
	PA_OutputText(1,0,0,"GetTile(x, y) : %d", i);
	
	checkChest(direction); // checks for chests only along the side of the screen that the player is moving toward
	/*
	
	//===Actual Collision Detection===//
	switch(direction)
	{
		case 1: // right
			if ( CheckCol(50,60) )
				moving = false;
			break;
			
		case 2: // left
			if ( CheckCol(16,60) )
				
				moving = false;
			break;
			
		case 3: // up
			if ( CheckCol(32,55) )
				
				moving = false;
			break;
			
		case 4: // down
			if ( CheckCol(32,64) )
				
				moving = false;
			break;
	}//=====PAB=====//
	*/
	
	
	
	
/****** UPDATE SPRITE LOCATION *******/
//------------------------------------------------------------
	
	//get current frame to stop moonwalking
	u16 frame;
	frame = PA_GetSpriteAnimFrame(0, avSprNo);
	
	if(moving == true)	
	//do not allow sprite to move if the stylus is being used
	{
		//do not allow diagonal movement
		if(!((Pad.Held.Right)||(Pad.Held.Left)||(Pad.Held.A)||(Pad.Held.Y)))
		{
			//y += Pad.Held.Down - Pad.Held.Up;				// up/down movement
			//y += Pad.Held.B - Pad.Held.X;				// up/down movement
			
			PA_OutputText(1, 10, 10, "Frame:   %d", frame);
			//stop moonwalking
			//if((frame <= 20) && (frame >= 15))
			if((frame <= 20) && (frame >= 15) && (!(CheckCol(32,64))))
			{
				y += Pad.Held.Down;
				y += Pad.Held.B;
				
				//dotY += Pad.Held.Down; //for mini map
				//dotY += Pad.Held.B;
				/*
				//double run
				if(Pad.Held.Down && Pad.Held.B)
				{
					u8 runTime = 0;
					runTime = Tick(gameClock);
					//charge once ever second
					if((runTime + 1000) <= Tick(gameClock))
					{
						if(player.staminaCheck(1))
						{
						y += Pad.Held.Down;
						y += Pad.Held.B;
						}
					}
					else
					{
						y += Pad.Held.Down;
						y += Pad.Held.B;
					}
				}
				else
				{
					y += Pad.Held.Down;
					y += Pad.Held.B;
				}
				*/
			}
			
			//if((frame <= 61) && (frame >= 56)) 
			if((frame <= 61) && (frame >= 56) && (!(CheckCol(32,55))))
			{
			y -= Pad.Held.Up;
			y -= Pad.Held.X;
			
			//dotY -= Pad.Held.Up;
			//dotY -= Pad.Held.X;
			}
		}
		
		//do not allow diagonal movement
		if(!((Pad.Held.Down)||(Pad.Held.Up)||(Pad.Held.B)||(Pad.Held.X)))
		{
			//x += Pad.Held.Right - Pad.Held.Left;  // left/right movement
			//x += Pad.Held.A - Pad.Held.Y;  // left/right movement
			
			//if((frame <= 6) && (frame >= 1))
			if((frame <= 6) && (frame >= 1) && (!(CheckCol(50,60))))
			{
			x += Pad.Held.Right;
			x += Pad.Held.A;
			
			//dotX += Pad.Held.Right;
			//dotX += Pad.Held.A;
			}
			
			//if((frame <= 13) && (frame >= 8))
			if((frame <= 13) && (frame >= 8) && (!(CheckCol(16,60))))
			{
			x -= Pad.Held.Left;
			x -= Pad.Held.Y;
			
			//dotX -= Pad.Held.Left;
			//dotX -= Pad.Held.Y;
			}
		}
		/*
		//GUI
		//don't let sprite move down in this area
		if (y > (159 - PA_GetSpriteLy(0, avSprNo)))
			y = (159 - PA_GetSpriteLy(0, avSprNo));
		*/

		//PA_LargeScrollXY(0, 1, x, y);
		//PA_SetSpriteXY(0, avSprNo, x, y);				   // set sprite position
		miniMap();
		mapScrolling();
		loadMap();
		spawnControl();
		

		
		
		
		
	}
		
}


#endif //__DPAD_CONTROLS_H__
